package com.zy.composeworld.ui.screens.text

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.basicMarquee
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.requiredSize
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.text.selection.DisableSelection
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Add
import androidx.compose.material.icons.rounded.Menu
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shadow
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.ParagraphStyle
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.zy.composeworld.R


@OptIn(ExperimentalFoundationApi::class)
@Preview
@Composable
fun TextMainPager001() {

    LazyColumn(
        modifier = Modifier.padding(16.dp),
        verticalArrangement = Arrangement.spacedBy(16.dp),
    ) {
        item {
            Text("Hello World")
        }
        item {
            Text("显示资源中的文字:" + stringResource(R.string.compose))
        }
        item {
            Text("Hello World(FontStyle.Italic)", fontStyle = FontStyle.Italic)
        }
        item {
            Text("Hello World(FontStyle.Normal)", fontStyle = FontStyle.Normal)
        }

        item {
            Text("Hello World(FontWeight.Light)", fontWeight = FontWeight.Light)
        }

        item {
            Text("Hello World(FontWeight.Medium)", fontWeight = FontWeight.Medium)
        }

        item {
            Text("Hello World(FontWeight.Bold)", fontWeight = FontWeight.Bold)
        }

        item {
            Text("Hello World(FontWeight.Black)", fontWeight = FontWeight.Black)
        }

        item {
            Text("Hello World(fontSize 12.sp)", fontSize = 12.sp)
        }

        item {
            Text("Hello World(fontSize 15.sp)", fontSize = 15.sp)
        }
        item {
            Text("Hello World(fontSize 18.sp)", fontSize = 18.sp)
        }

        item {
            Text("Hello World(fontSize 24.sp)", fontSize = 24.sp)
        }

        item {
            Text("Hello World(Color.Blue)", color = Color.Blue)
        }
        item {
            Text("Hello World(Color.Red)", color = Color.Red)
        }
        item {
            Text("Hello World(Color.Green)", color = Color.Green)
        }

        item {
            TextHead("letterSpacing - 设置字符间距")
        }


        item {
            Text("letterSpacing(1.sp)", letterSpacing = 1.sp)
        }
        item {
            Text("letterSpacing(2.sp)", letterSpacing = 2.sp)
        }
        item {
            Text("letterSpacing(4.sp)", letterSpacing = 4.sp)
        }

        item {
            TextHead("textDecoration - 设置下划线与删除线")
        }

        item {
            Text(
                text = "TextDecoration.Underline",
                //下划线
                textDecoration = TextDecoration.Underline,
            )
        }

        item {
            Text(
                text = "TextDecoration.LineThrough",
                //中划(删除/过时)线
                textDecoration = TextDecoration.LineThrough,
            )
        }
        item {
            TextHead("textAlign - 文本排序")

            Text(
                text = "TextAlign.Start\nHello",
                textAlign = TextAlign.Start,
                modifier = Modifier
                    .fillMaxWidth()
                    .height(100.dp)
                    .border(1.dp, color = Color.Blue)
            )

            Text(
                text = "TextAlign.Center\n" + "Hello",
                textAlign = TextAlign.Center,
                modifier = Modifier
                    .fillMaxWidth()
                    .height(100.dp)
                    .border(1.dp, color = Color.Blue)
            )

            Text(
                text = "TextAlign.End\n" + "Hello",
                textAlign = TextAlign.End,
                modifier = Modifier
                    .fillMaxWidth()
                    .height(100.dp)
                    .border(1.dp, color = Color.Blue)
            )

            Text(
                text = "TextAlign.Left\n" + "Hello",
                textAlign = TextAlign.Left,
                modifier = Modifier
                    .fillMaxWidth()
                    .height(100.dp)
                    .border(1.dp, color = Color.Blue)
            )

            Text(
                text = "TextAlign.Right\n" + "Hello",
                textAlign = TextAlign.Right,
                modifier = Modifier
                    .fillMaxWidth()
                    .height(100.dp)
                    .border(1.dp, color = Color.Blue)
            )

            Text(
                text = "TextAlign.Justify\n" + "Hello",
                textAlign = TextAlign.Justify,
                modifier = Modifier
                    .fillMaxWidth()
                    .height(100.dp)
                    .border(1.dp, color = Color.Blue)
            )
        }


        item {
            TextHead("lineHeight - 设置行高")
        }

        item {
            Text(
                "素胚勾勒出青花笔锋浓转淡(行高35.sp)",
                //行高
                lineHeight = 35.sp,
                //字符间距
                letterSpacing = 35.sp,
            )
        }
        item {
            Text(
                "素胚勾勒出青花笔锋浓转淡(行高50.sp)",
                //行高
                lineHeight = 50.sp,
                //字符间距
                letterSpacing = 35.sp,
            )
        }





        item {
            TextHead("buildAnnotatedString - 富文本")
            Text(modifier = Modifier.fillMaxWidth(), text = buildAnnotatedString {
                withStyle(
                    style = SpanStyle(
                        fontWeight = FontWeight.Bold, color = Color.Blue, fontSize = 20.sp
                    )
                ) {
                    append("H")
                }
                append("ello\t")
                withStyle(
                    style = SpanStyle(
                        fontWeight = FontWeight.Bold, color = Color.Green, fontSize = 20.sp
                    )
                ) {
                    append("W")
                }
                append("orld")
            })

            Text(buildAnnotatedString {
                withStyle(style = SpanStyle(color = Color.Blue)) {
                    append("H")
                }
                append("ello ")

                withStyle(style = SpanStyle(fontWeight = FontWeight.Bold, color = Color.Red)) {
                    append("W")
                }
                append("orld")
            })

            Text(
                buildAnnotatedString {
                    withStyle(style = ParagraphStyle(lineHeight = 30.sp)) {
                        withStyle(style = SpanStyle(color = Color.Blue)) {
                            append("Hello\n")
                        }
                        withStyle(
                            style = SpanStyle(
                                fontWeight = FontWeight.Bold, color = Color.Red
                            )
                        ) {
                            append("World\n")
                        }
                        append("Compose")
                    }
                }
            )
        }

        item {
            TextHead("Brush - 使用画笔设置文本样式")
            val gradientColors = listOf(Color.Cyan, Color.Blue, Color.Green)

            Text(
                text = "linearGradient - 使用画笔设置文本样式xt", style = TextStyle(
                    brush = Brush.linearGradient(colors = gradientColors)
                )
            )

            Text(
                text = "horizontalGradient - 使用画笔设置文本样式xt", style = TextStyle(
                    brush = Brush.horizontalGradient(colors = gradientColors)
                )
            )

            Text(
                text = "verticalGradient - 使用画笔设置文本样式xt", style = TextStyle(
                    brush = Brush.verticalGradient(colors = gradientColors)
                )
            )
            Text(
                text = "radialGradient - 使用画笔设置文本样式xt", style = TextStyle(
                    brush = Brush.radialGradient(colors = gradientColors)
                )
            )

            Text(
                text = "sweepGradient - 使用画笔设置文本样式xt", style = TextStyle(
                    brush = Brush.sweepGradient(colors = gradientColors)
                )
            )

            Text(text = buildAnnotatedString {
                append("Do not allow people to dim your shine\n")
                withStyle(
                    SpanStyle(
                        brush = Brush.linearGradient(
                            colors = gradientColors
                        )
                    )
                ) {
                    append("because they are blinded.")
                }
                append("\nTell them to put some sunglasses on.")
            })

            val brush = Brush.linearGradient(colors = gradientColors)
            Text(text = buildAnnotatedString {
                withStyle(
                    SpanStyle(
                        brush = brush, alpha = .3f
                    )
                ) {
                    append("文本跨度中的不透明度 ")
                }
                withStyle(
                    SpanStyle(
                        brush = brush, alpha = .5f
                    )
                ) {
                    append("Text in ")
                }
                withStyle(
                    SpanStyle(
                        brush = brush, alpha = 1f
                    )
                ) {
                    append("Compose ❤️")
                }
            })

        }


        item {
            TextHead("添加阴影")
            TextShadow()
            TextShadow2()
        }


        item {
            TextHead("长按复制与过滤复制")
            TextCopy()
        }

        item {
            TextHead("跑马灯")
            Column(Modifier.width(400.dp)) {
                Text(
                    "Learn about why it's great to use Jetpack Compose",
                    modifier = Modifier.basicMarquee(),
                    fontSize = 50.sp
                )
            }
        }

        item {
            TextHead("限制可见行数")
            Text("Hello ".repeat(50), maxLines = 2)
            Text("World ".repeat(50), maxLines = 3)
        }

        item {
            TextHead("overflow - 指示文字溢出")

            Text("Hello Compose ".repeat(50), maxLines = 2, overflow = TextOverflow.Ellipsis)
            Text("Hello Compose ".repeat(50), maxLines = 2, overflow = TextOverflow.Clip)
            Text("Hello Compose ".repeat(50), maxLines = 2, overflow = TextOverflow.Visible)
        }


    }

}

@Composable
fun TextShadow() {
    val offset = Offset(5.0f, 10.0f)
    Text(
        text = "Hello world!", style = TextStyle(
            fontSize = 24.sp, shadow = Shadow(
                color = Color.Blue, offset = offset, blurRadius = 3f
            )
        )
    )
}

@Composable
fun TextShadow2() {
    val offset = Offset(-5.0f, -10.0f)
    Text(
        text = "Hello world!", style = TextStyle(
            fontSize = 24.sp, shadow = Shadow(
                color = Color.Green, offset = offset, blurRadius = 10f
            )
        )
    )
}

@Composable
fun TextCopy() {
    SelectionContainer(modifier = Modifier.fillMaxWidth()) {
        Column {
            Text(
                "SelectionContainer添加复制内容",
                modifier = Modifier.fillMaxWidth(),
                textAlign = TextAlign.Center
            )
            Text(
                "天青色等烟雨", modifier = Modifier.fillMaxWidth(), textAlign = TextAlign.Center
            )
            Text(
                "而我在等你", modifier = Modifier.fillMaxWidth(), textAlign = TextAlign.Center
            )
            Text(
                "月色被打捞起", modifier = Modifier.fillMaxWidth(), textAlign = TextAlign.Center
            )
            //过滤复制
            DisableSelection {
                Text(
                    "跳过复制", modifier = Modifier.fillMaxWidth(), textAlign = TextAlign.Center
                )
                Text(
                    "晕开了结局", modifier = Modifier.fillMaxWidth(), textAlign = TextAlign.Center
                )
                Text(
                    "如传世的青花瓷",
                    modifier = Modifier.fillMaxWidth(),
                    textAlign = TextAlign.Center
                )
            }
            Text(
                "自顾自美丽", modifier = Modifier.fillMaxWidth(), textAlign = TextAlign.Center
            )
            Text(
                "你眼带笑意", modifier = Modifier.fillMaxWidth(), textAlign = TextAlign.Center
            )
        }
    }
}

@Composable
private fun TextHead(text: String) {
    Text(text, fontWeight = FontWeight.Bold, color = Color.Red)
}